home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_kdelibs.idb / usr / freeware / kde / include / kprocess.h.z / kprocess.h
Encoding:
C/C++ Source or Header  |  1999-01-26  |  18.1 KB  |  568 lines

  1. /* This file is part of the KDE libraries
  2.     Copyright (C) 1997 Christian Czezakte (e9025461@student.tuwien.ac.at)
  3.  
  4.     This library is free software; you can redistribute it and/or
  5.     modify it under the terms of the GNU Library General Public
  6.     License as published by the Free Software Foundation; either
  7.     version 2 of the License, or (at your option) any later version.
  8.  
  9.     This library is distributed in the hope that it will be useful,
  10.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  12.     Library General Public License for more details.
  13.  
  14.     You should have received a copy of the GNU Library General Public License
  15.     along with this library; see the file COPYING.LIB.  If not, write to
  16.     the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  17.     Boston, MA 02111-1307, USA.
  18. */
  19. //
  20. //  KPROCESS -- A class for handling child processes in KDE without
  21. //  having to take care of Un*x specific implementation details
  22. //
  23. //  version 0.3.1, Jan 8th 1998
  24. //
  25. //  (C) Christian Czezatke
  26. //  e9025461@student.tuwien.ac.at
  27. //
  28.  
  29. #ifndef __kprocess_h__
  30. #define __kprocess_h__
  31.  
  32. #include <sys/types.h> // for pid_t
  33. #include <sys/wait.h>
  34. #include <signal.h>
  35. #include <unistd.h>
  36. #include <qstrlist.h>
  37. #include <qobject.h>
  38. #include <qsocknot.h>
  39.  
  40.  
  41. /**
  42.   @short KProcess -- A class for invoking Child processes from within KDE applications
  43.   @author Christian Czezakte e9025461@student.tuwien.ac.at
  44.  
  45. 1) General usage and features
  46.  
  47.   This class allows a KDE application to start child processes without having
  48.   to worry about UN*X signal handling issues and zombie process reaping
  49.  
  50.   Basically, this class distinguishes three different ways of running
  51.   child processes: 
  52.  
  53.   +) "KProcess::DontCare" -- The child process is invoked and both the child
  54.   process and the parent process continue concurrently. 
  55.  
  56.   Starting a "DontCare" child process means that the application is
  57.   not interested in any notification to determine whether the
  58.   child process has already exited or not.
  59.   
  60.   +) "KProcess::NotifyOnExit" -- The child process is invoked both the
  61.   child and the parent process run concurrently.
  62.   
  63.   When the child process exits, the KProcess instance
  64.   corresponding to it emits the Qt Signal "processExited".
  65.   
  66.   Since this signal is _not_ emitted from within a UN*X
  67.   signal handler, arbitrary function calls can be made.
  68.   
  69.   +) "KProcess::Block" -- The child process starts and the parent
  70.   process is suspended 
  71.   until the child process exits. (_Really_ not recommended for programs
  72.   with a GUI)
  73.  
  74.   KProcess also provides several functions for determining the exit status
  75.   and the pid of the child process it represents. 
  76.  
  77.   Furthermore it is possible to supply command-line arguments to the process
  78.   in a clean fashion (no null -- terminated stringlists and such...)
  79.  
  80.   A small usage example:
  81.   <pre>
  82.   KProcess proc;
  83.  
  84.   proc << "my_executable";
  85.   proc << "These" << "are" << "the" << "command" << "line" << "args";
  86.   QApplication::connect(&proc, SIGNAL(processExited(KProcess *)), 
  87.                         pointer_to_my_object, SLOT(my_objects_slot));
  88.   proc.start();
  89.   </pre>  
  90.  
  91.   This will start "my_executable" with the commandline arguments "These"...
  92.   
  93.   When the child process exits, the respective Qt signal will be emitted.
  94.   
  95.   2) Communication with the child process
  96.   
  97.   KProcess supports communication with the child process through
  98.   stdin/stdout/stderr.
  99.   
  100.   The following functions are provided for getting data from the child process
  101.   or sending data to the child's stdin (For more information, have a look at the 
  102.   documentation of each function):
  103.   
  104.   
  105.   bool writeStdin(char *buffer, int buflen);
  106.   -- Transmit data to the child process's stdin.
  107.   
  108.   bool closeStdin();
  109.   -- Closes the child process's stdin (which causes it to see a "feof(stdin)")
  110.   Returns FALSE if you try to close stdin for a process that has been started
  111.   without a communication channel to stdin.
  112.   
  113.   QT signals:
  114.   
  115.   void receivedStdout(KProcess *proc, char *buffer, int buflen);
  116.   void receivedStderr(KProcess *proc, char *buffer, int buflen);
  117.   -- Indicates that new data has arrived from either the
  118.   child process's stdout or stderr.
  119.   
  120.   void wroteStdin(KProcess *proc);
  121.   -- Indicates that all data that has been sent to the child process
  122.   by a prior call to "writeStdin" has actually been transmitted to the
  123.   client 
  124. */
  125. class KProcess : public QObject
  126. {
  127.   Q_OBJECT
  128.  
  129. public:
  130.  
  131.   /** enums for communication channels to open. If communication for more 
  132.       than one channel is required, the values have to be or'ed together, 
  133.       for example to get communication with stdout as well as with
  134.       stdin, you would specify "Stdin | Stdout" 
  135.   */
  136.   enum Communication { NoCommunication = 0, Stdin = 1, Stdout = 2, Stderr = 4,
  137.                        AllOutput = 6, All = 7 };
  138.  
  139.   /** various run--modes for a child process. For more information about the
  140.       semantics of the run modes have a look at the general description of
  141.       the @ref KProcess class.
  142.   */
  143.   enum RunMode { DontCare, NotifyOnExit, Block };
  144.  
  145.   /** Constructor */
  146.   KProcess();
  147.  
  148.   /**
  149.          Destructor:
  150.  
  151.       If the process is running when the destructor for this class
  152.       is called, the child process is killed with a SIGKILL, but
  153.       only if the run mode is not of type "DontCare". --
  154.       Processes started as "DontCare" keep running anyway...
  155.   */
  156.   virtual ~KProcess();
  157.  
  158.   /**
  159.          The use of this function is now depreciated. -- Please use the
  160.      "operator<<" instead of "setExecutable".
  161.  
  162.      Sets the executable to be started with this KProcess object.
  163.      Returns FALSE if the process is currently running (in that
  164.      case the executable remains unchanged.) 
  165.  
  166.   */
  167.   bool setExecutable(const char *proc);
  168.  
  169.  
  170.   /** Sets the executable and the command line argument list for this process 
  171.       
  172.       For exsmple, doning a "ls -l /usr/local/bin" can be achieved by:
  173.       <pre>
  174.       KProcoess p;
  175.       ...
  176.       p << "ls" << "-l" << "/usr/local/bin"
  177.       </pre>
  178.  
  179.    */
  180.   KProcess &operator<<(const char *arg);
  181.  
  182.   /** Clears a command line argument list that has been set by using the "operator<<".
  183.   */
  184.   void clearArguments();
  185.  
  186.   /** Starts up the process. -- For a detailed description of the 
  187.       various run modes and communication semantics, have a look at the 
  188.       general description of the KProcess class.
  189.  
  190.       This function returns TRUE if the process was started successfully.
  191.       The following problems could cause KProcess:start" to return FALSE:
  192.  
  193.       +) the process is already running
  194.  
  195.       +) the command line argument list is empty 
  196.  
  197.       +) the starting of the process failed (could not fork)
  198.  
  199.       The second argument specifies which communication links should be
  200.       established to the child process. (stdin/stdout/stderr). By default,
  201.       no communication takes place and the respective communication signals 
  202.       will never get emitted.
  203.   */
  204.   virtual bool start(RunMode  runmode = NotifyOnExit, Communication comm = NoCommunication);
  205.  
  206.   /**
  207.      Stops the process (by sending a SIGTERM to it). -- You may send other
  208.      signals too of course... ;-) ) 
  209.  
  210.      Returns TRUE if the signal could be delivered successfully
  211.   */
  212.   virtual bool kill(int signo = SIGTERM);
  213.  
  214.   /**
  215.      Returns TRUE if the process is (still) considered to be running
  216.   */
  217.   bool isRunning();
  218.  
  219.   /** Returns the process id of the process. If it is called after
  220.       the process has exited, it returns the process id of the last
  221.       child process that was created by this instance of KProcess.
  222.  
  223.       Calling it before any child process has been started by this
  224.       KProcess instance causes getPid to return 0
  225.   */    
  226.   pid_t getPid();
  227.  
  228.  
  229.   /** Returns TRUE if the process has already finished and has exited
  230.       "voluntarily", ie: it has not been killed by a signal.
  231.   */
  232.   bool normalExit();
  233.  
  234.   /** Returns the exit status of the process. Please use "KProcess::normalExit" to 
  235.       check whether the process has exited cleanly (KProcess::normalExit returning
  236.       TRUE) before calling this function because if the process did not exit
  237.       normally, it does not have a valid exit status.
  238.   */
  239.   int  exitStatus(); 
  240.  
  241.    
  242.   /**
  243.      Transmit data to the child process's stdin. KProcess::writeStdin
  244.          may return FALSE in the following cases:
  245.  
  246.          +) The process is not currently running
  247.  
  248.      +) Communication to stdin has not been requested in the "start" call
  249.  
  250.          +) transmission of data to the child process by a previous call to "writeStdin"
  251.             is still in progress. 
  252.  
  253.      Please note that the data is sent to the client asynchronousely,
  254.      so when this function returns, the data might not have been
  255.      processed by the child process. 
  256.  
  257.      If all the data has been sent to the client, the signal
  258.      "wroteStdin" will be emitted. 
  259.  
  260.      Please note that you must not free "buffer" or call "writeStdin"
  261.      again until either a "wroteStdin" signal indicates that the data has been sent or a
  262.      "processHasExited" signal shows that the child process is no
  263.      longer alive... 
  264.   */
  265.   bool writeStdin(char *buffer, int buflen);
  266.  
  267.   /**
  268.      This causes the stdin file descriptor of the child process to be
  269.      closed indicating an "EOF" to the child. This function will
  270.      return FALSE if:
  271.  
  272.      +) No communication to the process's stdin has been specified in the "start" call.
  273.   */
  274.   bool closeStdin();
  275.  
  276.   signals: 
  277.  
  278.   /**
  279.      This signal gets emitted after the process has terminated when
  280.      the process was run in the "NotfiyOnExit"  (=default option to
  281.      "start") or the "Block" mode. 
  282.   */     
  283.   void processExited(KProcess *proc);
  284.  
  285.  
  286.   /**
  287.      These signals get emitted, when output from the child process has
  288.      been received on stdout. -- To actually get
  289.      these signals, the respective communication link (stdout/stderr)
  290.      has to be turned on in "start". 
  291.  
  292.      "buffer" contains the data, and "buflen" bytes are available from
  293.      the client. 
  294.  
  295.      You should copy the information contained in "buffer" to your private
  296.      data structures before returning from this slot.
  297.   */
  298.   void receivedStdout(KProcess *proc, char *buffer, int buflen);
  299.  
  300.  
  301.   /**
  302.      These signals get emitted, when output from the child process has
  303.      been received on stderr. -- To actually get
  304.      these signals, the respective communication link (stdout/stderr)
  305.      has to be turned on in "start". 
  306.  
  307.      "buffer" contains the data, and "buflen" bytes are available from
  308.      the client. 
  309.  
  310.      You should copy the information contained in "buffer" to your private
  311.      data structures before returning from this slot.
  312.   */
  313.   void receivedStderr(KProcess *proc, char *buffer, int buflen);
  314.  
  315.   /**
  316.      This signal gets emitted after all the data that has been
  317.      specified by a prior call to "writeStdin" has actually been
  318.      written to the child process. 
  319.   */
  320.   void wroteStdin(KProcess *proc);
  321.  
  322.  
  323. protected slots:
  324.  
  325.  /**
  326.    This slot gets activated when data from the child's stdout arrives.
  327.    It usually calls "childOutput"
  328.   */
  329.   void slotChildOutput(int fdno);
  330.  
  331.  /**
  332.    This slot gets activated when data from the child's stderr arrives.
  333.    It usually calls "childError"
  334.   */
  335.   void slotChildError(int fdno);
  336.   /*
  337.     Slot functions for capturing stdout and stderr of the child 
  338.   */
  339.  
  340.  
  341.   /**
  342.     Called when another bulk of data can be sent to the child's
  343.     stdin. If there is no more data to be sent to stdin currently
  344.     available, this function must disable the QSocketNotifier "innot".
  345.   */
  346.   void slotSendData(int dummy);
  347.  
  348. protected:
  349.  
  350.   /**
  351.      The list of the process' command line arguments. The first entry
  352.      in this list is the executable itself.
  353.   */
  354.   QStrList arguments;
  355.   /**
  356.      How to run the process (blocking, notify_on_exit, dontcare). You should
  357.      not modify this data member directly from derived classes.
  358.   */
  359.   RunMode run_mode;
  360.   /** 
  361.      TRUE if the process is currently running. You should not 
  362.      modify this data member directly from derived classes. For
  363.      reading the value of this data member, please use "isRunning()"
  364.      since "runs" will probably be made private in later versions
  365.      of KProcess.
  366.   */
  367.   bool runs;
  368.  
  369.   /** 
  370.       The PID of the currently running process (see "getPid()").
  371.       You should not modify this data member in derived classes.
  372.       Please use "getPid()" instead of directly accessing this
  373.       member function since it will probably be made private in
  374.       later versions of KProcess.
  375.   */
  376.  
  377.   pid_t pid;
  378.  
  379.   /** The process' exit status as retrned by "waitpid". You should not 
  380.       modify the value of this data member from derived classes. You should
  381.       rather use "getStatus()" than accessing this data member directly
  382.       since it will probably be made private in further versions of
  383.       KProcess.
  384.   */
  385.   int status;
  386.  
  387.  
  388.   /*
  389.     Functions for setting up the sockets for communication.
  390.     setupCommunication 
  391.     -- is called from "start" before "fork"ing.
  392.     commSetupDoneP
  393.     -- completes communcation socket setup in the parent
  394.     commSetupDoneC
  395.     -- completes communication setup in the child process
  396.     commClose
  397.     -- frees all allocated communication ressources in the parent
  398.     after the process has exited
  399.   */
  400.  
  401.   /**
  402.     This function is called from "KProcess::start" right before a "fork" takes 
  403.     place. According to
  404.     the "comm" parameter this function has to initialize the "in", "out" and
  405.     "err" data member of KProcess.
  406.  
  407.     This function should return 0 if setting the needed communication channels
  408.     was successful. 
  409.  
  410.     The default implementation is to create UNIX STREAM sockets for the communication,
  411.     but you could overload this function and establish a TCP/IP communication for
  412.     network communication, for example. 
  413.   */      
  414.   virtual int setupCommunication(Communication comm);
  415.  
  416.   /**
  417.      Called right after a (successful) fork on the parent side. This function
  418.      will usually do some communications cleanup, like closing the reading end
  419.      of the "stdin" communication channel.
  420.  
  421.      Furthermore, it must also create the QSocketNotifiers "innot", "outnot" and
  422.      "errnot" and connect their Qt slots to the respective KProcess member functions.
  423.  
  424.      For a more detailed explanation, it is best to have a look at the default
  425.      implementation of "setupCommunication" in @ref kprocess.cpp.
  426.   */
  427.   virtual int commSetupDoneP();
  428.  
  429.   /**
  430.      Called right after a (successful) for, but before an "exec" on the child
  431.      process' side. It usually just closes the unused communication ends of
  432.      "in", "out" and "err" (like the writing end of the "in" communication
  433.      channel.
  434.   */
  435.   virtual int commSetupDoneC();
  436.  
  437.  
  438.   /**
  439.      Immediately called after a process has exited. This function normally calls commClose
  440.      to close all open communication channels to this process and emits the "processExited"
  441.      signal (if the process was not running in the "DontCare" mode).
  442.   */
  443.   virtual void processHasExited(int state);
  444.   /**
  445.      Should clean up the communication links to the child after it has exited. Should
  446.      be called from "processHasExited".
  447.   */
  448.   virtual void commClose();
  449.   
  450.  
  451.   int out[2], in[2], err[2];
  452.   /* the socket descriptors for stdin/stdout/stderr */
  453.  
  454.   QSocketNotifier *innot, *outnot, *errnot;
  455.   /* The socket notifiers for the above socket descriptors */
  456.  
  457.   /**
  458.      Lists the communication links that are activated for the child process.
  459.      Should not be modified from derived classes.
  460.   */
  461.   Communication communication;        
  462.  
  463.  
  464.  
  465.   /**
  466.      Called by "slotChildOutput" this function copies data arriving from the
  467.      child process's stdout to the respective buffer and emits the signal
  468.      "receivedStderr"
  469.   */
  470.   int childOutput(int fdno);
  471.  
  472.   /**
  473.      Called by "slotChildOutput" this function copies data arriving from the
  474.      child process's stdout to the respective buffer and emits the signal
  475.      "receivedStderr"
  476.   */
  477.   int childError(int fdno);
  478.    
  479.   // information about the data that has to be sent to the child:
  480.  
  481.   char *input_data;  // the buffer holding the data
  482.   int input_sent;    // # of bytes already transmitted
  483.   int input_total;   // total length of input_data
  484.  
  485.   /**
  486.     @ref KProcessController is a friend fo KProcess because it has to have
  487.     access to various data members.
  488.   */
  489.   friend class KProcessController;
  490.  
  491.  
  492. private:
  493.   // Disallow assignment and copy-construction
  494.   KProcess( const KProcess& ) {};
  495.   KProcess& operator= ( const KProcess& ) { return *this; }
  496. };
  497.  
  498. /**
  499.   @short A class derived from @ref KProcess to start child processes through a shell
  500.   @author Christian Czezakte e9025461@student.tuwien.ac.at
  501.  
  502. This class is similar to @ref KProcess. The only difference is that KShellProcess runs 
  503. the specified executable through a UN*X shell so that standard shell mechanisms like
  504. wildcard matching, use of pipes and environment variable expansion will work.
  505.  
  506. For example, you could run commands like the following through KShellProcess:
  507. <pre>
  508.   ls ~/HOME/ *.lyx | sort | uniq |wc -l 
  509. </pre>
  510.  
  511. KShellProcess tries really hard to find a valid executable shell. Here is the 
  512. algorithm used for finding an executable shell:
  513.  
  514.        +) Try to use executable pointed to by the "SHELL" environment variable
  515.  
  516.        +) Try the executable pointed to by the "SHELL" environment variable with
  517. whitespaces stripped off
  518.  
  519.        +) "/bin/sh" as a last ressort.
  520. */
  521. class KShellProcess: public KProcess
  522. {
  523.   Q_OBJECT
  524.  
  525. public:
  526.  
  527.   /**
  528.       Constructor
  529.  
  530.       By specifying the name of a shell (like "/bin/bash") you can override
  531.       the mechanism for finding a valid shell as described in the detailed
  532.       description of this class.
  533.   */
  534.   KShellProcess(const char *shellname=NULL);
  535.   ~KShellProcess();
  536.  
  537.   /** 
  538.     Starts up the process. -- For a detailed description
  539.     have a look at the "start" member function and the detailed
  540.     description of @ref KProcess .
  541.  
  542.   */
  543.   virtual bool start(RunMode  runmode = NotifyOnExit, Communication comm = NoCommunication);
  544.  
  545. private:
  546.  
  547.   /** searches for a valid shell. See the general description of this class for
  548.       information on how the search is actually performed.
  549.   */
  550.   char *searchShell();
  551.  
  552.   /** used by "searchShell" in order to find out whether the shell found is actually
  553.       executable at all.
  554.   */
  555.   bool isExecutable(const char *fname);
  556.  
  557.  
  558.   char *shell;
  559.  
  560.   // Disallow assignment and copy-construction
  561.   KShellProcess( const KShellProcess& ) {};
  562.   KShellProcess& operator= ( const KShellProcess& ) { return *this; }
  563. };
  564.  
  565.  
  566.  
  567. #endif
  568.